package com.eoner.baselibrary.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.eoner.baselibrary.R;
import com.eoner.baselibrary.utils.ScreenUtils;

/**
 * 作者：sean
 * 创建时间：2017/8/23 17:25
 * 功能说明：
 */
public class PermissionDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private String title;
    private String desc;
    private int imgResource;


    public PermissionDialog(@NonNull Context context, String title, String desc,int imgResource,AgreementPrivacy agreementPrivacy) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.title = title;
        this.desc = desc;
        this.imgResource = imgResource;
        this.agreementPrivacy = agreementPrivacy;
    }

    public PermissionDialog(@NonNull Context context, String title, String desc,int imgResource,PermissionClickListener permissionClickListener) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.title = title;
        this.desc = desc;
        this.imgResource = imgResource;
        this.permissionClickListener = permissionClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_permission);
        setCanceledOnTouchOutside(false);

        TextView tvTitle = findViewById(R.id.tv_title);
        TextView tvDesc = findViewById(R.id.tv_desc);
        TextView tvAgreement = findViewById(R.id.tv_agreement);
        ImageView ivClose = findViewById(R.id.iv_close);
        ImageView ivHead = findViewById(R.id.iv_permission_head);
//        ivHead.setImageResource(imgResource);
        tvTitle.setText(title);
        tvDesc.setText(desc);

        tvAgreement.setOnClickListener(this);
        ivClose.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.width = ScreenUtils.getScreenWidth();
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowParams);
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (permissionClickListener != null){
                permissionClickListener.onCancel();
            }
            dismiss();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    private AgreementPrivacy agreementPrivacy;
    private PermissionClickListener permissionClickListener;

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_agreement) {
            if (agreementPrivacy != null){
                agreementPrivacy.onAgreement();
            }
            if (permissionClickListener != null){
                permissionClickListener.onConfirm();
            }
            dismiss();
        } else if (id == R.id.iv_close) {
            if (permissionClickListener != null){
                permissionClickListener.onCancel();
            }
            dismiss();
        }
    }

    public interface AgreementPrivacy {
        void onAgreement();
    }

    public interface PermissionClickListener {
        void onConfirm();
        void onCancel();
    }
}
